<?php

/**
 * 焦点图图片管理
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: FocusImgController.class.php v1.0 2014-06-23 17:27:03 $
 */
namespace Admin\Controller;
class FocusImgController extends CommonController{
    private $model;
    private $service;
    public function _initialize() {
        parent::_initialize();
        $this->model = D("FocusImg");
        $this->service = D("FocusImg", "Service");
    }
    
    public function index() {
        $fid = I("get.fid", 0, "intval");
        if($fid <= 0){
            $this->error("无效操作！");
        }
        $list = $this->service->getByFid($fid);
        //var_dump($list);
        $this->assign("fid", $fid);
        $this->assign("list", $list);
        $this->display();
    }
    /**
     * 添加图片
     */
    public function add() {
        $fid = I("get.fid", 0, "intval");
        $FocusService = D("Focus", "Service");
        $AllFocus = $FocusService->getAll();
        if(!isArrayAndNotEmpty($AllFocus)){
            $this->error("请先添加焦点图！", U("Focus/add"));
        }
        $FocusList = array();
        foreach ($AllFocus as $val) {
            $FocusList[$val['id']] = $val['title'];
        }
        $this->assign("fid", $fid);
        $this->assign("focuslist", $FocusList);
        $this->display();
    }
    
    /**
     * 创建
     * @return type
     */
    public function create() {
        $imgs = I("post.img", null);
        if(!isArrayAndNotEmpty($imgs)){
            $this->errorReturn("无效操作！");
        }
        
        $result = $this->service->add($imgs);
        if(!$result['status']){
            return $this->errorReturn($result["data"]["error"]);
        }
        return $this->successReturn("添加图片成功", U("index", array("fid" => $imgs['fid'])));
    }
    
    /**
     * 修改
     */
    public function edit() {
        $id = I("get.id", 0, "intval");
        if($id <= 0){
            return $this->error("无效操作！");
        }
        $imgs = $this->service->getById($id);
        if(!isArrayAndNotEmpty($imgs)){
            return $this->error("无效操作！");
        }
        $FocusService = D("Focus", "Service");
        $AllFocus = $FocusService->getAll();
        if(!isArrayAndNotEmpty($AllFocus)){
            $this->error("请先添加焦点图！", U("Focus/add"));
        }
        $FocusList = array();
        foreach ($AllFocus as $val) {
            $FocusList[$val['id']] = $val['title'];
        }
        //var_dump($imgs);die;
        $this->assign($imgs);
        $this->assign("focuslist", $FocusList);
        $this->display();
    }
    
    /**
     * 更新
     */
    public function update() {
        $imgs = I("post.img", null);
        if(!isArrayAndNotEmpty($imgs)){
            $this->errorReturn("无效操作！");
        }
        
        $result = $this->service->update($imgs);
        if(!$result['status']){
            return $this->errorReturn($result["data"]["error"]);
        }
        return $this->successReturn("更新图片成功", U("index", array("fid" => $imgs['fid'])));
    }
    /**
     * 删除
     * @return type
     */
    public function delete() {
        $fid = I("get.fid", 0, "intval");
        $act = I("post.act", "one");
        if($act == "one"){
            $id = I("post.id", 0, "intval");
            if($id <= 0){
                return $this->errorReturn("无效操作！");
            }
            $imgs = $this->service->getById($id);
            if(!isArrayAndNotEmpty($imgs)){
                return $this->errorReturn("无效操作！");
            }
            if($this->service->delete($id)){
                //RmRecurse($imgs['path']);
                return $this->successReturn("删除成功", U("index"));
            }else{
                return $this->errorReturn("删除失败！");
            }
        }else if($act == "two"){
            $ids = I("post.ids");
            if(!isArrayAndNotEmpty($ids)){
                return $this->errorReturn("无效操作！");
            }
            foreach ($ids as $id) {
                $imgs = $this->service->getById($id);
                if(!isArrayAndNotEmpty($imgs)){
                    return $this->errorReturn("您要删除的数据不存在！");
                }
                $this->service->delete($id);
                //RmRecurse($imgs['path']);
            }
            return $this->successReturn('删除数据成功！', U('index', array('fid' => $fid)));
        }else{
            return $this->errorReturn("无效操作！");
        }
    }
}
